package com.mars.rabbitmq.delay;

import com.google.common.collect.Maps;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @Author King
 * @create 2020/8/19 10:32
 */
@Data
@Slf4j
@Configuration
public class DelayOrderRabbitMqConfig {

    public static final String DELAY_EXCHANGE = "delayMarsOrderExchange";
    public static final String DELAY_QUEUE = "delayMarsOrderQueue";
    public static final String DELAY_ROUTING_KEY = "delayMarsOrderRoutingKey";


    @Bean
    public DirectExchange delayKingOrderExchange() {
        return new DirectExchange(DELAY_EXCHANGE);
    }

    @Bean
    public Queue delayKingOrderQueue() {
        Map<String, Object> params = Maps.newHashMap();

        // x-dead-letter-exchange 声明了队列里的死信转发到的DLX名称，
        params.put("x-dead-letter-exchange", OrderRabbitMqConfig.EXCHANGE);

        // x-dead-letter-routing-key 声明了这些死信在转发时携带的 routing-key
        params.put("x-dead-letter-routing-key", OrderRabbitMqConfig.ROUTING_KEY);

        // 设置队列中消息的过期时间，单位 毫秒
        params.put("x-message-ttl", 10 * 1000);

        return new Queue(DELAY_QUEUE, true, false, false, params);
    }

    @Bean
    public Binding delayKingOrderBinding() {
        return BindingBuilder.bind(delayKingOrderQueue()).to(delayKingOrderExchange()).with(DELAY_ROUTING_KEY);
    }

}
